package com.kasinf.sample.websocket.mq;

import com.kasinf.framework.cache.utils.RedisUtils;
import com.kasinf.framework.common.util.json.JsonUtils;
import com.kasinf.framework.websocket.core.sender.WebSocketMessageSender;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @Author : Kaiser
 * @Date: 2025/6/23  16:42
 */
@Slf4j
@Component
public class RedisWebSocketMessageConsumer {

    @PostConstruct
    public void init() {
        RedisUtils.subscribe("redis-websocket-message", RedisWebSocketMessage.class, this::onMessage);
    }

    @Resource
    private RedisWebSocketMessageSender webSocketMessageSender;

    public void onMessage(RedisWebSocketMessage message){
        log.info("收到 Websocket 消息: {}", JsonUtils.toJsonString(message));
        webSocketMessageSender.send(message.getSessionId(), message.getUserType(), message.getUserId(), message.getMessageType(), message.getMessageContent());
    }
}
